# Contributor: Matt Dainty <matt+alpine@bodgit-n-scarper.com>
# Contributor: Dermot Bradley <dermot_bradley@yahoo.com>
# Maintainer: Dermot Bradley <dermot_bradley@yahoo.com>
pkgname=cloud-init
pkgver=22.2.2
pkgrel=0
pkgdesc="Cloud instance init scripts"
url="https://cloud-init.io"
# Dependant package "cloud-utils-growpart" is not available for
# s390x so disable this arch for this package too.
arch="noarch !s390x"
license="Apache-2.0 OR GPL-3.0-only"
depends="
	blkid
	cloud-utils-growpart
	eudev
	ifupdown-ng
	iproute2-minimal
	lsblk
	parted
	python3
	py3-configobj
	py3-jinja2
	py3-jsonpatch
	py3-jsonschema
	py3-requests
	py3-yaml
	sfdisk
	sgdisk
	shadow
	tzdata
	"
makedepends="py3-setuptools"
checkdepends="
	bash
	coreutils
	grep
	py3-httpretty
	py3-netifaces
	py3-pytest
	py3-pytest-mock
	py3-responses
	"
subpackages="
	$pkgname-bash-completion
	$pkgname-doc
	$pkgname-openrc
	"
source="cloud-init-$pkgver.tar.gz::https://github.com/canonical/cloud-init/archive/$pkgver.tar.gz
	01-oauthlib-remove.patch
	02-disable-irrelevant-modules.patch
	03-hook-hotplug-sh.patch
	04-add-doas.patch
	cloud-init-hotplugd
	cloud-init-hotplugd.initd
	cloud-init.logrotate
	interfaces
	setup-cloud-init
	README.Alpine
	"
install="$pkgname.post-install"

# secfixes:
#   22.2.2-r0:
#     - CVE-2022-2084
#   21.1-r1:
#     - CVE-2021-3429

prepare() {
	default_prepare
}

build() {
	python3 setup.py build
}

check() {
	python3 -m pytest
}

package() {
	python3 setup.py install \
		--prefix=/usr --root="$pkgdir" --init-system=sysvinit_openrc

	mkdir -p \
		"$pkgdir"/etc/network
	install -m644 \
		"$srcdir"/interfaces \
		"$pkgdir"/etc/network/

	install -m755 \
		-D \
		"$srcdir"/cloud-init-hotplugd.initd \
		"$pkgdir"/etc/init.d/cloud-init-hotplugd

	install -m644 \
		-D \
		"$srcdir"/cloud-init.logrotate \
		"$pkgdir"/etc/logrotate.d/cloud-init

	mkdir -p \
		"$pkgdir"/sbin
	install -m755 \
		"$srcdir"/setup-cloud-init \
		"$pkgdir"/sbin/

	mkdir -p \
		"$pkgdir"/usr/sbin
	install -m755 \
		"$srcdir"/cloud-init-hotplugd \
		"$pkgdir"/usr/sbin/

	install -m644 \
		"$srcdir"/README.Alpine \
		"$pkgdir"/usr/share/doc/"$pkgname"/

	# Delete non-Alpine distribution template files
	for distro in \
		arch debian fedora freebsd gentoo opensuse photon redhat \
		rhel sles suse ubuntu
	do
		for file in \
			"$pkgdir"/etc/cloud/templates/*."$distro".tmpl
		do
			rm "$file"
		done
	done

	# Delete systemd-related files
	rm \
		"$pkgdir"/etc/cloud/templates/timesyncd.conf.tmpl \
		"$pkgdir"/etc/cloud/templates/systemd.resolved.conf.tmpl
	rm -Rf "$pkgdir"/etc/systemd

	# Delete NetworkManager-related files
	rm -Rf "$pkgdir"/etc/NetworkManager

	# Delete cloud-init modules not relevant to Alpine
	for module in \
		cc_apt_*.py \
		cc_fan.py \
		cc_grub_dpkg.py \
		cc_landscape.py \
		cc_refresh_rmc_and_interface.py \
		cc_reset_rmc.py \
		cc_rh_subscription.py \
		cc_snap.py \
		cc_ssh_import_id.py \
		cc_spacewalk.py \
		cc_ubuntu_advantage.py \
		cc_ubuntu_drivers.py \
		cc_yum_add_repo.py \
		cc_zypper_add_repo.py
	do
		rm "$pkgdir"/usr/lib/python3.*/site-packages/cloudinit/config/$module
	done

	# Delete cloud-init docs not relevant to Alpine
	for doc in \
		cloud-config-add-apt-repos.txt \
		cloud-config-apt.txt \
		cloud-config-update-apt.txt \
		cloud-config-yum-repo.txt
	do
		rm "$pkgdir"/usr/share/doc/cloud-init/examples/$doc
	done
}

sha512sums="
bc8e76d6e862b620cbe2b04e2034dcfa9cd2f19586b4934b4d79ad7ce4b4e7a6679c677738e41942e318392729bbfcdd16e1767dfc34701140db36e3e90ccdb8  cloud-init-22.2.2.tar.gz
c14ce3b27c55d1c381e27b2898bb73da8a74c53480d496bdfc87318b69fb612678205ab5a686c1ce703ef5c2c7a8529f629c8f908aeff39524d36870a83c1929  01-oauthlib-remove.patch
945eb69143a6c35ecbef1dcdec483e8cacc0dcbea6365160ed6c251629c56676edd4494e66768fafd47e48346d3b0af986290ac85c9983fa3179b60cc066e7e9  02-disable-irrelevant-modules.patch
463d7ca00f576de75444e86d9b32e1452c824a9f2708b624549e7e003b330c96fbc78d44756efd95d1f954fa0633ce0c0b619e798cdb33b8c86799ee07554834  03-hook-hotplug-sh.patch
592cbbbe1520cf272a8721ed284a8a96c4077577d00a438f608ce1f35b9f18e3b7fdd9ac1fb44663981f80caf1ef1c87e4b384a986283c167a526a1b24c44d82  04-add-doas.patch
3c6c5189859633012fd389a8d8370cbd46ddd07f206d61bcf872f12105f340552073cbf28f28096109c158b641de1c8199afcfc7f751166a27d4089355e1dd7d  cloud-init.logrotate
2d7b80fc248ec18f20c4ed2fbe0f8d6ea85ae57a0ece8ecf8a095cf910c576807c19ab0453c00160b85253df5725cba90abec731cb54f441a509611115519b46  cloud-init-hotplugd
ab44fc51979a5da8569b08ed2f290a6610de6c36cf147af20f30e9224847623d3594c056a1f64c614598ac130e9bec92d566fb901024f806b768a89190c45f59  cloud-init-hotplugd.initd
48b25ec4457c2b3772a7d210033551d041749a0d1869818d888030e6df7fd9bbc13a38b95cf465de3d46d96881a722f94a337584ce48f280c4a52b819586563a  interfaces
b82e06afa27de5880245ed5593157b38e18a77eac5284b6f40095709edf16390322d86755651c36bbcc545f5f840c58a8f74593a785ef016a3aec2a4fe256ad3  setup-cloud-init
8af764e82246a6417d9604ad93c080f3d3b7442937e0ccfd1fe47f9b5b3e0d5d81c5319aae27bf832909da9411bdf15f670e8425fbc99ed53e519f8ab61e4925  README.Alpine
"
